package edu.neusoft.lyc.topic_two.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * User：lyc
 * Date：2017-11-26
 * Time：17:32
 * Description：
 */
public class HelloClient {
    public static void main(String[] args) {
        //连接服务器端的IP地址及端口
        String host = "127.0.0.1";
        Integer port = 8000;

        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap client = new Bootstrap();
        try {
            client.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new HelloClientInitializer());
            // 连接服务端的通道
            Channel channel = client.connect(host, port).sync().channel();
            // 控制台输入
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while (true) {
                String line = bufferedReader.readLine();
                if (line == null){
                    continue;
                }
                //向服务端发送数据
                channel.writeAndFlush(line);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}
